<template>
    <div class="task-square">
        <div class="section-header">
            <h2 class="section-title">
                <el-icon>
                    <List />
                </el-icon>{{ title }}
            </h2>
            <a v-if="showMore" href="#" class="section-more" @click.prevent="handleMoreClick">
                查看更多<el-icon>
                    <ArrowRight />
                </el-icon>
            </a>
        </div>

        <div class="task-grid">
            <el-card v-for="task in tasks" :key="task.id" class="task-card" :style="getCardStyle(task)" shadow="hover"
                @click="handleTaskClick(task)" @mousemove="handleCardHover">
                <div class="task-header">
                    <el-tag :type="getStatusType(task.status)" size="small">{{ task.status }}</el-tag>
                    <span class="task-type">{{ task.type }}</span>
                </div>
                <h3 class="task-title">{{ task.title }}</h3>
                <p class="task-content">{{ task.content }}</p>

                <!-- 新增奖励信息展示 -->
                <div v-if="task.reward" class="task-reward">
                    <el-tag type="warning" size="small">
                        <el-icon>
                            <Coin />
                        </el-icon>
                        {{ task.reward }} {{ task.rewardUnit || getDefaultRewardUnit(task.rewardType) }}
                    </el-tag>
                    <span class="reward-type">{{ task.rewardType }}</span>
                </div>

                <div class="task-footer">
                    <div class="task-meta">
                        <span class="task-time">
                            <el-icon>
                                <Clock />
                            </el-icon>
                            {{ formatTime(task.startTime) }} - {{ formatTime(task.endTime) }}
                        </span>
                        <span v-if="task.remainCount" class="task-count">
                            <el-icon>
                                <User />
                            </el-icon>
                            剩余{{ task.remainCount }}个名额
                        </span>
                    </div>
                    <el-button type="primary" size="small" @click.stop="handleAcceptTask(task)">
                        立即参与
                    </el-button>
                </div>
            </el-card>
        </div>
    </div>
</template>

<script setup lang="ts">
import { List, ArrowRight, User, Clock, Coin } from '@element-plus/icons-vue'
import dayjs from 'dayjs'
import { ElMessage } from 'element-plus'
import type { ITask, RewardType } from '@/types/task'
import { ref } from 'vue'

const props = defineProps({
    title: {
        type: String,
        default: '任务广场'
    },
    tasks: {
        type: Array as () => ITask[],
        required: true
    },
    showMore: {
        type: Boolean,
        default: true
    }
})

const emit = defineEmits(['taskClick', 'acceptTask', 'moreClick'])

const mousePosition = ref({ x: 0, y: 0 })

const handleCardHover = (event: MouseEvent) => {
    const card = event.currentTarget as HTMLElement
    const rect = card.getBoundingClientRect()
    mousePosition.value = {
        x: ((event.clientX - rect.left) / rect.width) * 100,
        y: ((event.clientY - rect.top) / rect.height) * 100
    }
}

const getCardStyle = (task: ITask) => {
    let gradientColor = ''
    switch (task.status) {
        case '待领取':
            gradientColor = 'linear-gradient(135deg, #fff8e6 0%, #ffeed6 100%)'
            break
        case '进行中':
            gradientColor = 'linear-gradient(135deg, #e6f7ff 0%, #d6f0ff 100%)'
            break
        case '已完成':
            gradientColor = 'linear-gradient(135deg, #f6ffed 0%, #e6ffd6 100%)'
            break
        default:
            gradientColor = 'linear-gradient(135deg, #f5f5f5 0%, #e8e8e8 100%)'
    }

    return {
        '--mouse-x': `${mousePosition.value.x}%`,
        '--mouse-y': `${mousePosition.value.y}%`,
        '--gradient-bg': gradientColor
    }
}

const getDefaultRewardUnit = (type?: RewardType) => {
    switch (type) {
        case '积分': return '积分'
        case '现金': return '元'
        case '实物': return '件'
        default: return ''
    }
}

const handleTaskClick = (task: ITask) => {
    emit('taskClick', task)
}

const handleAcceptTask = (task: ITask) => {
    if (task.remainCount && task.remainCount > 0) {
        emit('acceptTask', task)
        // ElMessage.success(`已成功参与任务：${task.title}`)
    } else {
        ElMessage.warning('该任务已没有剩余名额')
    }
}

const handleMoreClick = () => {
    emit('moreClick')
}

const getStatusType = (status: string) => {
    switch (status) {
        case '待领取': return 'warning'
        case '进行中': return 'success'
        case '已完成': return 'info'
        default: return ''
    }
}

const formatTime = (timeStr: string) => {
    return dayjs(timeStr).format('MM-DD HH:mm')
}
</script>

<style scoped>
.task-square {
    margin-bottom: 40px;
}

.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.section-title {
    margin: 0;
    font-size: 20px;
    color: #333;
    display: flex;
    align-items: center;
}

.section-title .el-icon {
    margin-right: 8px;
    font-size: 24px;
}

.section-more {
    display: flex;
    align-items: center;
    font-size: 14px;
    color: #666;
    text-decoration: none;
    transition: all 0.3s;
}

.section-more:hover {
    color: #409eff;
}

.section-more .el-icon {
    margin-left: 5px;
}

.task-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 20px;
}

.task-card {
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    height: 100%;
    display: flex;
    flex-direction: column;
    border: none;
    position: relative;
    overflow: hidden;
    background: var(--gradient-bg);
}

.task-card:hover {
    transform: translateY(-5px) scale(1.02);
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
}

.task-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 4px;
    background: linear-gradient(90deg, #409eff, #67c23a);
    transition: all 0.3s ease;
}

.task-card:hover::before {
    height: 6px;
}

.task-card[data-status="待领取"]::before {
    background: linear-gradient(90deg, #e6a23c, #f56c6c);
}

.task-card[data-status="进行中"]::before {
    background: linear-gradient(90deg, #67c23a, #409eff);
}

.task-card[data-status="已完成"]::before {
    background: linear-gradient(90deg, #909399, #c0c4cc);
}

.task-card::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: radial-gradient(circle at var(--mouse-x) var(--mouse-y),
            rgba(255, 255, 255, 0.3),
            transparent 70%);
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.task-card:hover::after {
    opacity: 1;
}

:deep(.el-card__body) {
    padding: 20px;
    height: 100%;
    display: flex;
    flex-direction: column;
    background: rgba(255, 255, 255, 0.9);
    margin-top: 4px;
    border-radius: 0 0 4px 4px;
    backdrop-filter: blur(2px);
    transition: all 0.3s ease;
}

.task-card:hover :deep(.el-card__body) {
    background: rgba(255, 255, 255, 0.95);
}

.task-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}

.task-type {
    font-size: 12px;
    color: #666;
}

.task-title {
    margin: 0 0 10px;
    font-size: 18px;
    color: #333;
}

.task-content {
    margin: 0 0 15px;
    font-size: 14px;
    color: #666;
    flex-grow: 1;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
}

.task-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: auto;
}

.task-meta {
    font-size: 12px;
    color: #999;
}

.task-meta .el-icon {
    margin-right: 3px;
    font-size: 12px;
}

.task-time,
.task-count {
    display: flex;
    align-items: center;
    margin-right: 8px;
}

.task-reward {
    margin: 10px 0;
    display: flex;
    align-items: center;
    gap: 8px;
}

.task-reward .el-tag {
    display: inline-flex;
    align-items: center;
}

.task-reward .el-icon {
    margin-right: 4px;
}

.reward-type {
    font-size: 12px;
    color: #666;
    background: rgba(255, 255, 255, 0.7);
    padding: 2px 6px;
    border-radius: 4px;
}

/* 调整内容区域间距 */
.task-content {
    margin-bottom: 10px;
}
</style>